#!/usr/bin/env bash

# --- 必须指定容器名`container_name` 和宿主机端口`port_host`

container_name=p2240_server_api_xxx
#image_name=registry.cn-hangzhou.aliyuncs.com/sun5zone/python36.kbase:v4
image_name=bode135/bdpy36:v2

port_host=2240
ssh_port_host=2$port_host

# 容器内部端口
port_container=80
ssh_port_container=22

# 备用端口
backup_port_host=3$port_host
backup_port_container=8080

# --- 工作路径
workdir=opt/server
mkdir -p $workdir

# --- 开机启动脚本
startup_file_path=opt/startup.sh

# --- 运行container, 指定[工作路径, 映射路径, 端口映射, 环境变量, 容器名, 启动脚本] 
docker run -itd \
--workdir /$workdir  \
-v $(pwd)/opt:/opt \
-v $(pwd)/$startup_file_path:/$startup_file_path \
-p $port_host:$port_container \
-p $ssh_port_host:$ssh_port_container \
-p $backup_port_host:$backup_port_container \
-e PYTHONIOENCODING=utf-8 \
-e LANG=C.UTF-8 \
--restart=always \
--name $container_name $image_name \
bash -c "bash /$startup_file_path"


docker exec -it $container_name /bin/bash -c "echo 'cd /opt/server' >> /root/.bashrc"


# --- 常用调试命令

# curl localhost:$port_host

# docker rm -f $container_name
# docker ps -a | grep $container_name
# docker logs $container_name
# docker restart $container_name

# docker exec -it $container_name bash
# pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple 
# service ssh restart

# --- 收集静态文件
# python manage.py collectstatic  --noinput


